//
//  MyRemindViewController.m
//  xpkc
//
//  Created by TopSageOSX on 15/1/20.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "MyRemindViewController.h"

@interface MyRemindViewController ()

@end

@implementation MyRemindViewController{
    NSInteger aimhour;
    NSInteger aimmin;
}

- (void)viewDidAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (IsIOS7) {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    self.title = @"我的提醒";
    self.navigationController.navigationBar.hidden = NO;
    // Do any additional setup after loading the view.
    [self creatView];
}
- (void)creatView{
    UIDatePicker *datepicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 3, 100, 40)];
    datepicker.layer.borderWidth = 1;
    datepicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    datepicker.datePickerMode = UIDatePickerModeTime;
    datepicker.timeZone = [NSTimeZone localTimeZone];
    [datepicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datepicker];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 230, 150, 40)];
    label.text = @"启动每日提醒";
    [self.view addSubview:label];
    UISwitch *onremind = [[UISwitch  alloc]initWithFrame:CGRectMake(200, 230, 60, 40)];
    [onremind addTarget:self action:@selector(editRemind:) forControlEvents:UIControlEventValueChanged];
    onremind.on;
    [self.view addSubview:onremind];
    
    UIButton *ADD = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ADD.frame = CGRectMake(0.4*SCREEN_WIDTH, 280, 0.2*SCREEN_WIDTH, 40);
    [ADD setTitle:@"启动" forState:UIControlStateNormal];
    [ADD addTarget:self action:@selector(addRemind) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:ADD];
    
    UIButton *CANCEL = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    CANCEL.frame = CGRectMake(0.4*SCREEN_WIDTH, 330, 0.2*SCREEN_WIDTH, 40);
    [CANCEL setTitle:@"停止" forState:UIControlStateNormal];
    CANCEL.layer.borderWidth = 1;
    [CANCEL addTarget:self action:@selector(cancelRemind) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:CANCEL];
    
    UIInterpolatingMotionEffect *motioneffect = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    motioneffect.minimumRelativeValue = [NSNumber numberWithInt:-40];
    motioneffect.maximumRelativeValue = [NSNumber numberWithInt:40];
    [CANCEL addMotionEffect:motioneffect];
}
- (void)editRemind:(UISwitch *)onremind{
    logdebug(@"%d",onremind.on);
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger currenthour;
    NSInteger currentmin;
    NSInteger currentsec;
    NSDate *date = [NSDate date];
    [calendar getHour:&currenthour minute:&currentmin second:&currentsec nanosecond:nil fromDate:date];
    logdebug(@"%d=%d=%d",currenthour,currentmin,currentsec);
    NSInteger sec = 60-currentsec;
    NSInteger min = aimmin-1-currentmin<0?60-currentmin:aimmin-1-currentmin;
    if (aimmin-1>currentmin) {
        aimhour -=1;
    }
    NSInteger hour = aimhour-currenthour<0?24-currenthour:aimhour-currenthour;
    date = [date initWithTimeInterval:sec+min*60+hour*3600 sinceDate:date];//启动时间
}
- (void)addRemind{
    logdebug(@"添加提醒");
    
    
    
    UILocalNotification *remind = [[UILocalNotification alloc]init];
    remind.applicationIconBadgeNumber = 1;
    remind.alertLaunchImage = @"Default";
    remind.alertBody = @"亲,该学习了";
    remind.alertAction = @"进入学派课程";
    remind.timeZone = [NSTimeZone defaultTimeZone];
    
    remind.soundName = UILocalNotificationDefaultSoundName;
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"remind" forKey:@"key"];
    remind.userInfo = dic;
    
    NSDate *date =  [NSDate dateWithTimeIntervalSinceNow:20];
    
    remind.fireDate = date;
    remind.repeatInterval = NSCalendarUnitDay;
    
    //
    [self cancelRemind];
    
    [[UIApplication sharedApplication] scheduleLocalNotification:remind];
    
    //TODO:appdelegate中添加
    /**
     *  3天未打开学习的
     *  1周未打开学习的
     *  1月未打开学习的
     */
}
- (void)cancelRemind{
    logdebug(@"删除提醒");
    NSArray *localreminds = [[UIApplication sharedApplication] scheduledLocalNotifications];
    BOOL had = NO;
    for (int i = 0; i<localreminds.count; i++) {
        UILocalNotification *remind = [localreminds objectAtIndex:i];
        if ([[remind.userInfo objectForKey:@"key"]isEqualToString:@"remind"]) {
            had = YES;
        }
        if (had) {
            [[UIApplication sharedApplication]cancelLocalNotification:remind];
            return;
        }
    }
}
- (void)dateChange:(UIDatePicker *)datepicker{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    [calendar getHour:&aimhour minute:&aimmin second:nil nanosecond:nil fromDate:datepicker.date];
    logdebug(@"目标时间:%d:%d",aimhour,aimmin);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
